<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$officer_id = getPostValue( 'officer_id' );
$from_army = getPostValue( "from_army" );
$to_army = getPostValue( "to_army" );

$query = "SELECT ruler_id, name " .
		"FROM officers " .
		"WHERE officer_id = " . $officer_id;
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$name = $row[ 'name' ];
if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$query = "SELECT transfer_id " .
			"FROM transfer " .
			"WHERE officer_id = " . $officer_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if( !isset( $row[ 'transfer_id' ] ) ) {
		$game_turn = getCurrentTurn();
		$query = "SELECT army_id, ruler_id " .
				"FROM army " .
				"WHERE officer_id = " . $officer_id;
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		
		if( isset( $from_army ) && $from_army > 0 ) {
			if( isset( $_POST[ 'remove_officer' ] ) ) {
				if( isset( $row[ 'army_id' ] ) && $row[ 'army_id' ] == $from_army && $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
				    $query = "INSERT INTO transfer " .
				    		"( ruler_id, officer_id, from_army_id, complete_turn ) " .
				    		"VALUES ( " . $_SESSION[ 'ruler_id' ] . ", $officer_id, $from_army, " . ( $game_turn + 6 ) . " )";
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				}
			}
		}
		elseif( isset( $to_army ) && $to_army > 0 ) {
			$query2 = "SELECT x, y, travel " .
					"FROM army " .
					"WHERE army_id = '$to_army'";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$x = $row2[ 'x' ];
			$y = $row2[ 'y' ];
			
			if( $row2[ 'travel ' ] == 0 ) {
				$query2 = "SELECT ruler_id " .
						"FROM village " .
						"WHERE x = $x AND y = $y";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
				if( $row2[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
					if( !isset( $row[ 'army_id' ] ) ) {
					    $query = "INSERT INTO transfer " .
					    		"( ruler_id, officer_id, to_army_id, complete_turn ) " .
					    		"VALUES ( " . $_SESSION[ 'ruler_id' ] . ", $officer_id, $to_army, " . ( $game_turn + 8 ) . " )";
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					}
				}
			}
		}
	}
}
header( "Location: officer.php?name=$name" );
?>